import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import UploadFile from  '@/components/Upload/uploadFile'
import { saveAs } from 'file-saver';
import axios from 'axios';
import methods from "./methods"
import {validPassWord} from "@/utils/validate";
import {uploadFile} from "@/api/upload"
import {validReg} from '@/utils/validate';
export default {
  name: "index",
  components: {
    Pagination,
    Editor,
    Toolbar,
    UploadFile
  },
  data() {
    const validate = (rule, value, callback) => {
      if (!this.taskFrom.fileUrl) {
        callback(new Error('请上传论文'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'required',
      trainingId: this.$route.query.trainingId,
      detail: {},
      endTime: 0,
      endTimeArr: [],
      endTimeCount: "",
      tcDetail: {},
      requiredListQuery: {
        pageNum: 1,
        pageSize: 10,
        navStatus: "0"
      },
      electiveListQuery: {
        pageNum: 1,
        pageSize: 10,
        navStatus: "0"
      },
      examListQuery: {
        pageNum: 1,
        pageSize: 10,
        navStatus: "0"
      },
      practiceListQuery: {
        pageNum: 1,
        pageSize: 10,
        navStatus: "0"
      },
      questionnaireListQuery: {
        pageNum: 1,
        pageSize: 10,
        navStatus: "0"
      },
      materialListQuery: {
        pageNum: 1,
        pageSize: 10,
        navStatus: "0"
      },
      requiredTotal: 0,
      electiveTotal: 0,
      examTotal: 0,
      practiceTotal: 0,
      questionnaireTotal: 0,
      materialTotal: 0,
      requiredList: [],
      electiveList: [],
      questionnaireList: [],//调查问卷
      examList: [],//考试
      practiceList: [],//练习
      materialList: [],//资料
      taskNotice: {},//作业须知
      taskList: [],//作业列表
      taskTotal: 0,
      refresh: localStorage.getItem("resetTrainFn"),
      showMoreTask: true,//;展开培训任务
      taskVisible: false,
      taskFrom: {
        name: "",
        content: "",
        fileUrl: "",
        filePath: "",
        id:"",
      },
      statusMap:{
        'create':'新增',
        'update':'编辑',
        'preview':'预览',
      },
      statusText:"create",
      taskFromDisableFlag:false,
      taskFromEditFlag:false,
      /*富文本*/
      editor: null,
      toolbarConfig: {
        excludeKeys: ['emotion', 'group-video'],
      },
      editorConfig: { placeholder: '请输入内容...' },
      mode: 'simple', // or 'simple'
      editLength: 0,
      taskFromRules: {
        name: [{ "required": true, "message": "请输入论文名称", "trigger": "blur" }],
        content: [{ "required": true, validator: validate, "trigger": "blur" }],
        fileUrl: [{ "required": true, validator: validate, "trigger": "blur" }],
      },
      uploadFile:uploadFile,

      studyWindow: null,
      studyCount: 0,

      //折算学时
      convertList: [],
      convertVisible: false,
      convertArea: [],
      convertEdit:false,

      converVisible: false,
      converEdit:false,
      formReadonly:false,
      convertFrom: {
        id:"",
        recordId:"",
        convertCredit: "",
        convertType: "",
        trainingTime: "",
        remark: "",
        title: "",
        trainingOrg: "",
        trainingYear: "",
        trainingContent: "",
        auditReason: "",
        resourceList: [],
      },
      convertFromRules: {
        title: [{ "required": true, "message": "请输入培训名称", "trigger": "blur" }],
        trainingOrg: [{ "required": true, "message": "请输入培训机构", "trigger": "blur" }],
        trainingYear: [
          { "required": true, "message": "请输入培训年份", "trigger": "blur" },
          {pattern: validReg().numReg,"message": "培训年份只能输入数字"}
          ],
        convertCredit: [{ "required": true, "message": "请输入获得学时", "trigger": "blur", }],
        trainingContent: [{ "required": true, "message": "请输入培训内容", "trigger": "blur" }],
        convertType: [{ "required": true, "message": "请选择培训类别", "trigger": "blur" }],
        trainingTime: [{ "required": true, "message": "请选择培训时间", "trigger": "blur" }],
        // resourceList: [{ "required": true, "message": "请上传证明", "trigger": "blur" }],
      },
      convertType:[
        {
          label:"公需",
          value:"0",
        },
        {
          label:"专业",
          value:"1",
        },
        {
          label:"混合",
          value:"2",
        },
      ],
      showCertificate:false,
      certificateInfo:{},
      userCertRecord:{},
      userCertRecordFlag:false,
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '2': 'success',
        '1': 'primary',
        '0': 'warning',
        '3': 'danger'
      }
      return statusMap[status]
    },
    statusNameFilter(status) {
      const statusMap = {
        '0': '待提交',
        '1': '申请中',
        '2': '已通过',
        '3': '已驳回',
      };
      return statusMap[status]
    },
  },
  watch: {
    refresh: {
      handler: function (newVal, oldVal) {
        console.log(newVal, oldVal)
        if (newVal === 'changeList') {
          this.changeList();
          localStorage.removeItem("resetTrainFn");
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
   ...methods
  },
  mounted() {
    this.getDetail();
    this.getSchedule();
    var that = this;
    window.addEventListener('visibilitychange', function () {
      that.updateRefresh();
      if (document.visibilityState === 'visible') {
        that.updateRefresh();
      }
    })

    window.addEventListener('focus', this.updateRefresh);
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
}
